<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Member extends MX_Controller {

	private $error = array();

	function Member() {
		parent::__construct();
		$this->load->model('lookupmodel');
		$this->load->model('helpermodel');
		//check is logged in
		$this->load->model('languagemodel');
		$this->load->model('mdl_member');
		$this->load->model('email/emailmodel');
		$this->language = $this->languagemodel->is_language_using();
		$this->load->helper('MY_url_helper');
		$this->load->helper('utf8');
		$this->load->helper('text');
	}

	function index() {
		if (!$this->members->isLogged()) {
			redirect(base_url() . 'member/login', 'refresh');
		}
		 
		redirect(base_url() . 'member/dashboard', 'refresh');
	}

	function dashboard() {
		if (!$this->members->isLogged()) {
			redirect(base_url() . 'member/login', 'refresh');
		}
		 
		$this->lang->load('member/dashboard', $this->language);

		//SEO
		$data['title'] = $this->lang->line('text_title');
		$data['description'] = $this->lang->line('text_description');
		$data['keywords'] = $this->lang->line('text_keywords');

		//Text
		$data['text_heading'] = $this->lang->line('text_heading');
		
		if (($this->input->server('REQUEST_METHOD') == 'POST')) {
			$data['Title'] = $this->input->post('title', TRUE);
			$data['FirstName'] = $this->input->post('firstname', TRUE);
			$data['LastName'] = $this->input->post('lastname', TRUE);
			$data['MobileCountry'] = $this->input->post('mobile_country', TRUE);
			$data['Mobile'] = $this->input->post('phone', TRUE);
			$data['Email'] = $this->input->post('email', TRUE);
			
			$this->mdl_member->editCustomer($data);
			
			$this->session->set_userdata(array("success" => $this->lang->line('text_success')));
			
			redirect(base_url() . 'member/dashboard', 'location');
		}
		
		if ($this->session->userdata('success')) {
			$data['success'] = $this->session->userdata('success');

			$this->session->unset_userdata('success');
		} else {
			$data['success'] = FALSE;
		}

		$data['module'] = "member";
		$data['view_file'] = "dashboard";

		$data['breadcrumbs'][] = array(
			'text'      => 'Home',
			'href'      => base_url()
		);

		$data['breadcrumbs'][] = array(
			'text'      => $this->lang->line('text_heading'),
			'href'      => base_url() . 'member/dashboard'
		);
		
		if($this->session->userdata('success')) {
			$data['success'] = $this->session->userdata('success');
			$this->session->unset_userdata('success');
		} else {
			$data['success'] = "";
		}

		$data['logged'] = $this->members->isLogged();
		
		if ($this->input->server('REQUEST_METHOD') != 'POST') {
			$customer_info = $this->mdl_member->getCustomer($this->members->getId());
		}

		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}

		if ($this->input->post('title')) {
			$data['titleM'] = $this->input->post('title');
		} elseif (isset($customer_info)) {
			$data['titleM'] = $customer_info->Title;
		} else {
			$data['titleM'] = '';
		}

		if ($this->input->post('firstname')) {
			$data['firstname'] = $this->input->post('firstname');
		} elseif (isset($customer_info)) {
			$data['firstname'] = $customer_info->FirstName;
		} else {
			$data['firstname'] = '';
		}

		if ($this->input->post('lastname')) {
			$data['lastname'] = $this->input->post('lastname');
		} elseif (isset($customer_info)) {
			$data['lastname'] = $customer_info->LastName;
		} else {
			$data['lastname'] = '';
		}

		if ($this->input->post('mobile_country')) {
			$data['mobile_country'] = $this->input->post('mobile_country');
		} elseif (isset($customer_info)) {
			$data['mobile_country'] = $customer_info->MobileCountry;
		} else {
			$data['mobile_country'] = '';
		}

		if ($this->input->post('phone')) {
			$data['phone'] = $this->input->post('phone');
		} elseif (isset($customer_info)) {
			$data['phone'] = $customer_info->Mobile;
		} else {
			$data['phone'] = '';
		}

		if ($this->input->post('email')) {
			$data['email'] = $this->input->post('email');
		} elseif (isset($customer_info)) {
			$data['email'] = $customer_info->Email;
		} else {
			$data['email'] = '';
		}
		
		echo Modules::run('template/default_template', $data);
	}

	function getAllPropertiesByType() {
		$propertyClassType = $this->input->get('property_clss_type');
		 
		$json = array();
		 
		$propertytype_info = $this->lookupmodel->getPropertyTypeByType($this->config->item('config_language_id'), $propertyClassType, $this->config->item('market'));
		 
		echo json_encode($propertytype_info);
	}
	
	function getCities() {
		$propertyClassType = $this->input->get('location');
		if($propertyClassType == 0) {
			$marketID = 'ID';
		}
		 
		$json = array();
		 
		$propertytype_info = $this->lookupmodel->getListCities($this->config->item('config_language_id'), $marketID);
		 
		echo json_encode($propertytype_info);
	}
	
	function getInternationalCountries() {
		$propertyClassType = $this->input->get('location');
		 
		$json = array();
		 
		$propertytype_info = $this->lookupmodel->getInternationalCountries($this->config->item('config_language_id'), $propertyClassType);
		 
		echo json_encode($propertytype_info);
	}

	/*
	 * Get list all location by city id
	 *
	 * @access public
	 * @return json
	 */
	function getListAllLocation() {
		$json['localities'] = array();
		$citiesObj = array();

		$citiesVal = $this->input->get('citiesVal');
		if(!empty($citiesVal)) {
			$citiesObj = explode(',', $citiesVal);
		}

		if(isset($citiesObj) && is_array($citiesObj) && count($citiesObj) > 0) {
			//Case: 1: Only location
				
			//Case 2: Only Sub Area
				
			//Case 3: Both location and sub area
				
			//Case 4: No location and sub area
				
			foreach ($citiesObj as $item) {
				$city_info = $this->lookupmodel->getCity($this->config->item('config_language_id'), $item, $this->config->item('market'));

				$json['localities'][] = array(
					'City_ID' 	=> $city_info->City_ID,
					'Name'    	=> $city_info->Name,
					'zone'    	=> $this->lookupmodel->getLocationByCityId($this->config->item('config_language_id'), $item, $this->config->item('market'))
				);
			}
		}

		//echo "<pre>"; print_r($json['localities']); die();

		echo json_encode($json['localities']);
	}

	/*
	 * Get All Sub Area Using A Location Array
	 *
	 * @access public
	 * return json
	 */

	function getListAllSubArea() {
		$json['subarea'] = array();
		$locationObj = array();

		$locationVal = $this->input->get('locationVal');
		if(!empty($locationVal)) {
			$locationObj = explode(',', $locationVal);
		}

		if(isset($locationObj) && is_array($locationObj) && count($locationObj) > 0) {
			foreach ($locationObj as $item) {
				$location_info = $this->lookupmodel->getLocationInfo($this->config->item('config_language_id'), $item, $this->config->item('market'));

				$json['subarea'][] = array(
					'Location_ID' 	=> $location_info->Location_ID,
					'Name'    	=> $location_info->Name,
					'zone'    	=> $this->lookupmodel->getSubAreaByLocationId($this->config->item('config_language_id'), $item, $this->config->item('market'))
				);
			}
		}

		echo json_encode($json['subarea']);
	}

	function getBaliSubArea() {
		$json['subarea'] = array();
		$locationObj = array();

		$locationVal = $this->input->get('locationVal');
		if(!empty($locationVal)) {
			$locationObj = explode(',', $locationVal);
		}

		if(isset($locationObj) && is_array($locationObj) && count($locationObj) > 0) {
			foreach ($locationObj as $item) {
				$city_info = $this->lookupmodel->getCity($this->config->item('config_language_id'), $item, $this->config->item('market'));

				$json['subarea'][] = array(
					'City_ID' 	=> $city_info->City_ID,
					'Name'    	=> $city_info->Name,
					'zone'    	=> $this->lookupmodel->getSubAreaByLocationId($this->config->item('config_language_id'), $item, $this->config->item('market'))
				);
			}
		}

		echo json_encode($json['subarea']);
	}

	function create() {
		$data = $this->get_data_from_post();
		$data['module'] = "member";
		$data['view_file'] = "form";
		echo Modules::run('template/two_col', $data);
	}

	function getUserAgent() {
		//User Agent Definitions
		$this->load->library('user_agent');

		if ($this->agent->is_browser())
		{
			$agent = $this->agent->browser().' '.$this->agent->version();
		}
		elseif ($this->agent->is_robot())
		{
			$agent = $this->agent->robot();
		}
		elseif ($this->agent->is_mobile())
		{
			$agent = $this->agent->mobile();
		}
		else
		{
			$agent = 'Unidentified User Agent';
		}

		return $agent;
	}

	function get_data_from_post() {
		$data['Title'] = $this->input->post('title', TRUE);
		$data['UserName'] = $this->input->post('firstname', TRUE);
		$salt = substr(md5(uniqid(rand(), true)), 0, 9);
		$data['PassWord'] = $this->input->post('password', TRUE);
		$data['PassWord'] = sha1($salt . sha1($salt . sha1($data['PassWord'])));
		$data['Salt'] = $salt;
		$data['FirstName'] = $this->input->post('firstname', TRUE);
		$data['LastName'] = $this->input->post('lastname', TRUE);
		$data['MobileCountry'] = $this->input->post('mobile_country', TRUE);
		$data['Mobile'] = $this->input->post('phone', TRUE);
		$data['Email'] = $this->input->post('email', TRUE);
		$data['ShortList'] = NULL;
		$data['IpAddress'] = $this->input->server('REMOTE_ADDR');
		$data['UserAgent'] = $this->getUserAgent();
		$data['IsApproved'] = 1;
		$data['CreatedDate'] = date("Y-m-d H:i:s");
		$data['UpdatedDate'] = date("Y-m-d H:i:s");
		$data['MemberGroupFID'] = 1;
		$data['MarketFID'] = $this->config->item('market');
		//$data['IpAddress'] =

		return $data;
	}

	function submit() {
		$this->load->library('form_validation');
		$this->form_validation->set_rules('Member_ID', 'Member ID', 'required|numeric|xss_clean');
		$this->form_validation->set_rules('UserName', 'UserName', 'required|min_length|3|xss_clean');
		$this->form_validation->set_rules('PassWord', 'PassWord', 'required');

		if ($this->form_validation->run() == FALSE) {
			$this->create();
		} else {
			$data = $this->get_data_from_post();
			$this->_insert($data);
			redirect('member');
		}
	}

	function login() {
		if ($this->members->isLogged()) {
        	$redirect = $this->input->get('redirect');
			if($redirect) {
				redirect($redirect);
			} else {
				redirect(base_url() . 'member/dashboard');
			}
		}
		 
		$this->lang->load('member/login', $this->language);

		//SEO
		$data['title'] = $this->lang->line('text_title');
		$data['description'] = $this->lang->line('text_description');
		$data['keywords'] = $this->lang->line('text_keywords');

		//Text
		$data['text_heading'] = $this->lang->line('heading_title');

		$data['module'] = "member";
		$data['view_file'] = "login";

		$data['breadcrumbs'][] = array(
			'text'      => 'Home',
			'href'      => base_url()
		);

		$data['breadcrumbs'][] = array(
			'text'      => $this->lang->line('text_account'),
			'href'      => base_url() . 'member/dashboard'
		);

		$data['breadcrumbs'][] = array(
			'text'      => $this->lang->line('text_login'),
			'href'      => base_url() . 'member/login'
		);

		$server = $this->input->server('REQUEST_METHOD');

		if($server == "POST" && $this->validateMemberLogin()) {
         	$redirect = $this->input->get('redirect');
                        if($redirect)
                            redirect($redirect);
                        else
                            redirect(base_url() . 'member/dashboard', 'refresh');
		}

		if ($this->session->userdata('success')) {
			$data['success'] = $this->session->userdata('success');

			$this->session->unset_userdata('success');
		} else {
			$data['success'] = FALSE;
		}
		
		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}

		echo Modules::run('template/default_template', $data);
	}

	function logout() {
		if ($this->members->isLogged()) {
			$this->members->logout();

			$this->session->unset_userdata('wishlist');
			redirect(base_url(), 'refresh');
		}
		 
		$this->lang->load('member/logout', $this->language);

		//SEO
		$data['title'] = $this->lang->line('text_title');
		$data['description'] = $this->lang->line('text_description');
		$data['keywords'] = $this->lang->line('text_keywords');

		//Text
		$data['text_heading'] = $this->lang->line('heading_title');

		$data['module'] = "member";
		$data['view_file'] = "logout";

		$data['breadcrumbs'][] = array(
			'text'      => 'Home',
			'href'      => base_url()
		);

		$data['breadcrumbs'][] = array(
			'text'      => $this->lang->line('text_account'),
			'href'      => base_url() . 'member/dashboard'
		);

		$data['breadcrumbs'][] = array(
			'text'      => $this->lang->line('text_logout'),
			'href'      => base_url() . 'dashboard/logout'
		);

		echo Modules::run('template/default_template', $data);
	}

	function password() {
		if (!$this->members->isLogged()) {
			redirect(base_url() . 'member/login');
		}
		 
		$this->lang->load('member/password', $this->language);

		//SEO
		$data['title'] = $this->lang->line('text_title');
		$data['description'] = $this->lang->line('text_description');
		$data['keywords'] = $this->lang->line('text_keywords');

		//Text
		$data['text_heading'] = $this->lang->line('heading_title');

		$data['module'] = "member";
		$data['view_file'] = "password";

		$data['breadcrumbs'][] = array(
			'text'      => 'Home',
			'href'      => base_url()
		);

		$data['breadcrumbs'][] = array(
			'text'      => $this->lang->line('text_account'),
			'href'      => base_url() . 'member/dashboard'
		);

		$data['breadcrumbs'][] = array(
			'text'      => $this->lang->line('heading_title'),
			'href'      => base_url() . 'member/password'
		);

		$server = $this->input->server('REQUEST_METHOD');

		if($server == "POST") {
			$this->mdl_member->editPassword($this->members->getEmail(), $this->input->post('password'));
			
			$this->session->set_userdata(array("success" => $this->lang->line('text_success')));
			
			redirect(base_url() . 'member/dashboard', 'location');
		}

		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}

		if ($this->input->post('password')) {
			$data['password'] = $this->input->post('password');
		} else {
			$data['password'] = '';
		}

		if ($this->input->post('confirm')) {
			$data['confirm'] = $this->input->post('confirm');
		} else {
			$data['confirm'] = '';
		}
		
		echo Modules::run('template/default_template', $data);
	}

	function newsletter() {
		if (!$this->members->isLogged()) {
			redirect(base_url() . 'member/login');
		}
		 
		$this->lang->load('member/newsletter', $this->language);

		//SEO
		$data['title'] = $this->lang->line('text_title');
		$data['description'] = $this->lang->line('text_description');
		$data['keywords'] = $this->lang->line('text_keywords');

		//Text
		$data['text_heading'] = $this->lang->line('heading_title');

		$data['module'] = "member";
		$data['view_file'] = "newsletter";

		$data['breadcrumbs'][] = array(
			'text'      => 'Home',
			'href'      => base_url()
		);

		$data['breadcrumbs'][] = array(
			'text'      => $this->lang->line('text_account'),
			'href'      => base_url() . 'member/dashboard'
			);

			$data['breadcrumbs'][] = array(
			'text'      => $this->lang->line('heading_title'),
			'href'      => base_url() . 'member/newsletter'
			);

			$server = $this->input->server('REQUEST_METHOD');

			if($server == "POST" && $this->validateMemberLogin()) {
				redirect(base_url() . 'member/dashboard', 'refresh');
			}

			if (isset($this->error['warning'])) {
				$data['error_warning'] = $this->error['warning'];
			} else {
				$data['error_warning'] = '';
			}

			if ($this->input->post('password')) {
				$data['password'] = $this->input->post('password');
			} else {
				$data['password'] = '';
			}

			if ($this->input->post('confirm')) {
				$data['confirm'] = $this->input->post('confirm');
			} else {
				$data['confirm'] = '';
			}

			echo Modules::run('template/default_template', $data);
	}

	function validateMemberLogin() {
		$result = $this->members->login($this->input->post('email'), $this->input->post('password'));
		if(!$result) {
			$this->error['warning'] = $this->lang->line('error_login');
		}

		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}

	function register() {
		$this->lang->load('member/register', $this->language);
		$this->lang->load('mail/customer', $this->language);
		 
		$this->input->ip_address();

		//SEO
		$data['title'] = $this->lang->line('text_title');
		$data['description'] = $this->lang->line('text_description');
		$data['keywords'] = $this->lang->line('text_keywords');

		//Text
		$data['text_heading'] = $this->lang->line('heading_title');
		$this->load->model('mdl_member');

		$isRegister = $this->input->post('action');

		if(isset($isRegister) && $isRegister == "isRegister" && $this->validate()) {
			$this->load->model('mdl_member');
			$data = $this->get_data_from_post();
			$insert_id = $this->mdl_member->_insert($data);
			 
			if($insert_id > 0) {
				//send email
				$to = $data['Email'];
				
				$from = $this->lang->line('config_name');
				$bcc = "";
				$subject = sprintf($this->lang->line('text_subject'), $this->config->item('config_name'));
				
				$text = $this->lang->line('text_login') . "\n\n";
				$text .= base_url() . 'member/login' . "\n\n";
				
				$message = $this->lang->line('text_login') . "<br/><br/>";
				$message .= base_url() . 'member/login' . "\n\n";
				
				$success = $this->emailmodel->sendmail($to, $from, $bcc, $subject, $message, $text);
				redirect(base_url() . 'member/success', 'refresh');
			}
		}

		$data['module'] = "member";
		$data['view_file'] = "register";

		$data['breadcrumbs'][] = array(
			'text'      => 'Home',
			'href'      => base_url()
		);

		$data['breadcrumbs'][] = array(
			'text'      => $this->lang->line('text_account'),
			'href'      => base_url() . 'member/dashboard'
		);

		$data['breadcrumbs'][] = array(
			'text'      => $this->lang->line('text_register'),
			'href'      => base_url() . 'member/register'
		);

		if (isset($this->error['warning'])) {
			$data['error_warning'] = $this->error['warning'];
		} else {
			$data['error_warning'] = '';
		}

		if ($this->input->post('title')) {
			$data['titleM'] = $this->input->post('title');
		} else {
			$data['titleM'] = '';
		}

		if ($this->input->post('firstname')) {
			$data['firstname'] = $this->input->post('firstname');
		} else {
			$data['firstname'] = '';
		}

		if ($this->input->post('lastname')) {
			$data['lastname'] = $this->input->post('lastname');
		} else {
			$data['lastname'] = '';
		}

		if ($this->input->post('mobile_country')) {
			$data['mobile_country'] = $this->input->post('mobile_country');
		} else {
			$data['mobile_country'] = '';
		}

		if ($this->input->post('phone')) {
			$data['phone'] = $this->input->post('phone');
		} else {
			$data['phone'] = '';
		}

		if ($this->input->post('email')) {
			$data['email'] = $this->input->post('email');
		} else {
			$data['email'] = '';
		}

		if ($this->input->post('password')) {
			$data['password'] = $this->input->post('password');
		} else {
			$data['password'] = '';
		}

		if ($this->input->post('confirmpassword')) {
			$data['confirmpassword'] = $this->input->post('confirmpassword');
		} else {
			$data['confirmpassword'] = '';
		}

		echo Modules::run('template/default_template', $data);
	}

	function forgot_password() {
		if ($this->members->isLogged()) {
			redirect(base_url() . 'member/dashboard');
		}
		
		$this->lang->load('member/forgotten', $this->language);
		$this->lang->load('mail/forgotten', $this->language);

		//SEO
		$data['title'] = $this->lang->line('text_title');
		$data['description'] = $this->lang->line('text_description');
		$data['keywords'] = $this->lang->line('text_keywords');

		//Text
		$data['text_heading'] = $this->lang->line('heading_title');

		$data['module'] = "member";
		$data['view_file'] = "forgot_password";

		$data['breadcrumbs'][] = array(
			'text'      => 'Home',
			'href'      => base_url()
		);

		$data['breadcrumbs'][] = array(
			'text'      => $this->lang->line('text_account'),
			'href'      => base_url() . 'member/dashboard'
			);

		$data['breadcrumbs'][] = array(
			'text'      => $this->lang->line('text_forgotten'),
			'href'      => base_url() . 'member/forgot_password'
		);
		
		$server = $this->input->server('REQUEST_METHOD');

		if($server == "POST") {
			$this->load->model('mdl_member');
			
			$password = substr(sha1(uniqid(mt_rand(), true)), 0, 10);
			
			$this->mdl_member->editPassword($this->input->post('email'), $password);
			
			$to = $this->input->post('email');
				
			$from = $this->lang->line('config_name');
			$bcc = "";
			
			$subject = sprintf($this->lang->line('text_subject'), $this->config->item('config_name'));
			
			$text = $this->lang->line('text_password_reset');
			$text .= sprintf($this->lang->line('text_greeting'), $this->config->item('config_name')) . "\n\n";
			$text .= $this->lang->line('text_password') . "\n\n";
			$text .= $password;
			
			$message = $this->lang->line('text_password_reset') . "<br/><br/>";
			$message .= sprintf($this->lang->line('text_greeting'), $this->config->item('config_name')) . "<br/><br/>";
			$message .= $this->lang->line('text_password') . "<br/><br/>";
			$message .= $password;
			
			$success = $this->emailmodel->sendmail($to, $from, $bcc, $subject, $message, $text);
			
			$this->session->set_userdata(array("success" => $this->lang->line('text_success')));
			
			redirect(base_url() . 'member/login', 'location');
		}

		if ($this->input->post('email')) {
			$data['email'] = $this->input->post('email');
		} else {
			$data['email'] = '';
		}

		echo Modules::run('template/default_template', $data);
	}

	function success() {
		$this->lang->load('member/member', $this->language);

		//SEO
		$data['title'] = $this->lang->line('text_title');
		$data['description'] = $this->lang->line('text_description');
		$data['keywords'] = $this->lang->line('text_keywords');

		//Text
		$data['text_heading'] = $this->lang->line('text_login_heading');

		$data['module'] = "member";
		$data['view_file'] = "success";

		$data['breadcrumbs'][] = array(
			'text'      => 'Home',
			'href'      => base_url()
		);

		$data['breadcrumbs'][] = array(
			'text'      => $this->lang->line('text_login_heading'),
			'href'      => base_url() . 'member/login'
			);

			echo Modules::run('template/default_template', $data);
	}

	protected function validate() {
		$result = $this->mdl_member->getTotalMemberByEmail($this->input->post('email'));
		 
		if ($result) {
			$this->error['warning'] = $this->lang->line('error_exists');
		}
		 
		if (!$this->error) {
			return true;
		} else {
			return false;
		}
	}

	function getMemberByEmail() {
		$email = $this->input->post('emal');
	}

	function _insert($data) {
		$this->load->model('mdl_member');
		$this->mdl_member->_insert($data);
	}

}

?>